package com.bodu.system.controller;

import com.bodu.core.common.AbstractCommonController;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.result.PageResult;
import com.bodu.core.entity.result.Result;
import com.bodu.global.annon.TraceLog;
import com.bodu.global.security.LoginUserUtil;
import com.bodu.system.entity.po.FilePo;
import com.bodu.system.entity.request.FileRequest;
import com.bodu.system.entity.vo.FileVo;
import com.bodu.system.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 系统文件 API
 *
 * @author Administrator
 */
@Api(tags = "系统文件 API")
@RestController
@RequestMapping("/file")
public class FileController extends AbstractCommonController<FileService, FileRequest, FileVo> {

    private final FileService fileService;

    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @TraceLog()
    @ApiOperation("详情")
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(fileService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("保存系统文件")
    public Result<Integer> save(@RequestBody FileRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.save(fileService, request);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新系统文件")
    public Result<Integer> update(@RequestBody FileRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.update(fileService, request);
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("系统文件-查询")
    public Result<List<FilePo>> listPage(@RequestBody ParamReq req) {
        return super.listPage(fileService, req);
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("系统文件-统计")
    public Result<Integer> countPage(@RequestBody ParamReq req) {
        return super.countPage(fileService, req);
    }

    @TraceLog()
    @PostMapping("page")
    @ApiOperation("系统文件-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<FileRequest> req) {
        return super.page(fileService, req);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除系统文件")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return super.delete(fileService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除系统文件")
    @DeleteMapping("{ids}")
    public Result<Integer> batchDelete(List<Long> ids) {
        return super.batchDelete(fileService, ids);
    }

    /**
     * 上传文件
     * @param files
     * @return
     */
    @TraceLog()
    @ApiOperation("上传文件")
    @PostMapping({ "/upload"})
    public Result<Map<String, Object>> uploadFile(@RequestParam(value = "file") MultipartFile[] files) {
        return fileService.uploadFile(files);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }
}
